package com.education.user.pojo;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SendSmsUtil {

    //发送验证码
    public static SendSmsResponse sendSms(String phoneNumber, String verifyCode) throws ClientException {

        //设置超时时间(不必修改)
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        //(不必修改)
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化ascClient，("***"分别填写自己的AccessKey ID和Secret)
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tEJWDMJAUzWrSFguLy5", "5IbNWPh57GEGs1UXvmBpxAguNFT7jj");
        //(不必修改)
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
        //(不必修改)
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象(不必修改)
        SendSmsRequest request = new SendSmsRequest();
        //****处填写接收方的手机号码
        request.setPhoneNumbers(phoneNumber);
        //****填写已申请的短信签名
        request.setSignName("今软科技");
        //****填写获得的短信模版CODE
        request.setTemplateCode("SMS_191817239");
        //笔者的短信模版中有${code}, 因此此处对应填写验证码
        request.setTemplateParam("{\"code\":\""+verifyCode+"\"}");
        //不必修改
        return acsClient.getAcsResponse(request);
    }


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret)
                .setConnectTimeout(10000)
                .setReadTimeout(10000)
                .setRegionId("cn-hangzhou");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    //发送验证码
    public static String sendSmsTwo(String phoneNumber, String verifyCode) throws Exception {
        Client client = createClient("LTAI5tEJWDMJAUzWrSFguLy5", "5IbNWPh57GEGs1UXvmBpxAguNFT7jj");
        //com.aliyun.dysmsapi20170525.models.SendSmsRequest request = new com.aliyun.dysmsapi20170525.models.SendSmsRequest();
        ////****处填写接收方的手机号码
        //request.setPhoneNumbers(phoneNumber);
        ////****填写已申请的短信签名
        //request.setSignName("今软科技");
        ////****填写获得的短信模版CODE
        //request.setTemplateCode("SMS_191817239");
        ////笔者的短信模版中有${code}, 因此此处对应填写验证码
        //request.setTemplateParam("{\"code\":\""+verifyCode+"\"}");
        //com.aliyun.dysmsapi20170525.models.SendSmsResponse sendSmsResponse = client.sendSms(request);
        //return sendSmsResponse;

        /**
         * 1.发送短信
         */
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendReq = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName("今软科技")
                .setTemplateCode("SMS_191817239")
                .setTemplateParam("{\"code\":\""+verifyCode+"\"}");
        com.aliyun.dysmsapi20170525.models.SendSmsResponse sendResp = client.sendSms(sendReq);
        String code = sendResp.body.code;
        if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
            return sendResp.body.message;
        }
        return "验证码发送成功";

    }

}
